package com.ellricky.baselib.ui.student;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andr.common.ui.fragment.BaseToolBarFragment;
import com.andr.common.ui.mvp.BasePresenter;
import com.andr.common.util.ResUtil;
import com.andr.common.util.ViewUtil;
import com.andr.common.widget.tabvp.SimpleTabLayout;
import com.ellricky.baselib.BaseConstant;
import com.ellricky.baselib.R;
import com.ellricky.baselib.dialog.picker.EPickerEntityListener;
import com.ellricky.baselib.dialog.picker.EPickerUtil;
import com.ellricky.baselib.entity.IdNamePickerEntity;
import com.ellricky.baselib.listener.OnTakePhotoListener;
import com.ellricky.baselib.listener.StuManageListener;
import com.ellricky.baselib.ui.msg.MsgTipActivity;
import com.ellricky.baselib.util.EAppUtil;
import com.ellricky.baselib.util.EDialogUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

public class StudentManageFragment extends BaseToolBarFragment<Void, BasePresenter<Void>> implements StuManageListener {


    private SimpleTabLayout tabLayout;
    private ViewPager vp;

    private RelativeLayout rlAge;
    private RelativeLayout rlBirth;
    private RelativeLayout rlHour;
    private TextView tvAge;
    private TextView tvBirth;
    private TextView tvClassHour;
    private ImageView ivSearch;
    private EditText etSearch;

    private List<Fragment> frgList;

    private EPickerUtil ePickerUtil;
    private IdNamePickerEntity selAge;

    private String age = "";
    private String birthOrder = "";
    private String hourOrder = "";
    private String searchKey = "";

    public static StudentManageFragment newInstance() {
        StudentManageFragment fragment = new StudentManageFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public int getContentLayout() {
        return R.layout.base_activity_stu_manage;
    }

    @Override
    protected void initUIView(View root) {
        super.initUIView(root);
        ePickerUtil = new EPickerUtil(mContext);
        mTvTitle.setText(R.string.base_stu_manage);
        setRightBtn(0);

        tabLayout = root.findViewById(R.id.tabLayout);
        vp = root.findViewById(R.id.vp);

        rlAge = root.findViewById(R.id.rlAge);
        rlBirth = root.findViewById(R.id.rlBirth);
        rlHour = root.findViewById(R.id.rlHour);
        tvAge = root.findViewById(R.id.tvAge);
        tvBirth = root.findViewById(R.id.tvBirth);
        tvClassHour = root.findViewById(R.id.tvClassHour);
        ivSearch = root.findViewById(R.id.ib_search);
        etSearch =root. findViewById(R.id.etSearch);

        rlAge.setOnClickListener(this);
        rlBirth.setOnClickListener(this);
        rlHour.setOnClickListener(this);
        ivSearch.setOnClickListener(this);

        initTab();
    }


    private void initTab() {
        if (frgList == null) {
            // 在主页切换到其他tab后，再切回来，StudentFragment的实例不会改变，所以只需初始化一次就可以了
            frgList = new ArrayList<>();
            frgList.add(StudentFragment.newInstance(BaseConstant.STU_TYPE_VIP).setNumListener(this));
            frgList.add(StudentFragment.newInstance(BaseConstant.STU_TYPE_STOP).setNumListener(this));
            frgList.add(StudentFragment.newInstance(BaseConstant.STU_TYPE_NO).setNumListener(this));
            frgList.add(StudentFragment.newInstance(BaseConstant.STU_TYPE_RETURN).setNumListener(this));
            frgList.add(StudentFragment.newInstance(BaseConstant.STU_TYPE_CHAO).setNumListener(this));
        }
        List<String> tList = Arrays.asList(ResUtil.getStrArr(mContext, R.array.base_stu_manage));
        tabLayout.setUp(getChildFragmentManager(), frgList, tList, vp);
    }


    @Override
    protected void onClickView(View v) {
        super.onClickView(v);
        if (v == rlAge) {
            ePickerUtil.checkAgePicker(selAge, new EPickerEntityListener<IdNamePickerEntity>() {
                @Override
                public void onSelectItem(int pickType, IdNamePickerEntity nameValuePickerEntity) {
                    selAge = nameValuePickerEntity;
                    if (selAge != null) age = selAge.getName();
                    else age = "";
                    refreshData();
                }
            });

        } else if (v == rlBirth) {
            birthOrder = EAppUtil.getStuOrder(mContext, tvBirth, birthOrder);
            refreshData();
        } else if (v == rlHour) {
            hourOrder = EAppUtil.getStuOrder(mContext, tvClassHour, hourOrder);
            refreshData();
        }else if (v ==ivSearch){
            searchKey = etSearch.getText().toString().trim();
            refreshData();
        }
    }


    private void refreshData() {
        getCurFrg().updateFilterParam(age, birthOrder, hourOrder, false, searchKey);
    }


    @Override
    protected void onClickBtnRight() {
        super.onClickBtnRight();
        EDialogUtil.showStuManageDialog(mContext, new OnTakePhotoListener() {
            @Override
            public void onClickItem(DialogInterface dialogInterface, int item) {
                if (item == 0) EAppUtil.jumpStuApplyList(StudentManageFragment.this);
                else if (item == 1)
                    EAppUtil.jumpPageWithNoParam(StudentManageFragment.this, MsgTipActivity.class);
            }
        });
    }

    @Override
    protected void onClickBtnRightLeft() {
        super.onClickBtnRightLeft();
        EAppUtil.jumpPageWithNoParam(this, StudentSearchActivity.class);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == BaseConstant.RESULT_CODE_ADD) {
            refreshData();
        }
    }

    @Override
    public void updateTabNum(String formalNum, String stopNum, String noNum, String returnNum, String chaoNum) {
        Objects.requireNonNull(tabLayout.getTabAt(0)).setText("会员（" + formalNum + "人）");
        Objects.requireNonNull(tabLayout.getTabAt(1)).setText("停课（" + stopNum + "人）");
        Objects.requireNonNull(tabLayout.getTabAt(2)).setText("未续费（" + noNum + "人）");
        Objects.requireNonNull(tabLayout.getTabAt(3)).setText("退费（" + returnNum + "人）");
        Objects.requireNonNull(tabLayout.getTabAt(4)).setText("超期（" + chaoNum + "人）");
    }

    @Override
    public void resetFilterIcon() {
        age = "";
        birthOrder = "";
        hourOrder = "";
        ViewUtil.setTextViewDrawable(mConext, tvBirth, R.drawable.base_icon_order_nor, 2);
        ViewUtil.setTextViewDrawable(mConext, tvClassHour, R.drawable.base_icon_order_nor, 2);
    }

    private StudentFragment getCurFrg() {
        int index = tabLayout.getSelectedTabPosition();
        StudentFragment frg = (StudentFragment) frgList.get(index);
        return frg;
    }
}

